home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 126-150 / disk_147 / sys / prime / prime.zoo / fileio.c < prev    next >
C/C++ Source or Header  |  1988-07-25  |  11KB  |  439 lines

  1. /*
  2.  * Prime fileio.c for MicroGnuEmacs by Robert A. Larson
  3.  *    system dependent file io routines
  4.  *
  5.  * Prime keeps the parity bit of every character set, as does mg 2a for primos.
  6.  */
  7. #include <errd.ins.cc>
  8. #include <keys.ins.cc>
  9. #include "def.h"
  10.  
  11. static FILE    *ffp;
  12. char *index(), *rindex();
  13. fortran void cnam$$(), at$(), at$abs(), at$any(), at$hom(), clo$fu();
  14. fortran void gpath$(), dir$se(), cv$fdv();
  15. fortran long srsfx$();
  16.  
  17. /*
  18.  * Open a file for reading.
  19.  */
  20. ffropen(fn)
  21. char   *fn;
  22. {
  23.     if ((ffp=fopen(fn, "r")) == NULL)
  24.         return (FIOFNF);
  25.     return (FIOSUC);
  26. }
  27.  
  28. /*
  29.  * Open a file for writing.
  30.  * Return TRUE if all is well, and
  31.  * FALSE on error (cannot create).
  32.  */
  33. ffwopen(fn)
  34. char    *fn;
  35. {
  36.     short unit, type, sfu, code;
  37.     struct {short len; char data[128];} fnb;
  38.     register char *cp = &fnb.data[0];
  39.     struct {short len; char data[32];} basename;
  40.  
  41.     /* most of this is to get a SAM rather than DAM file ... */
  42.     (void) strcpy(fnb.data, fn);
  43.     fnb.len = strlen(fnb.data);
  44.     (void) srsfx$((short)(k$writ+k$getu+k$nsam), fnb, unit, type,
  45.         (short)0, (short)0, basename, sfu, code);
  46. #ifdef OPEN_BUG
  47.     if(code==0) {
  48.         clo$fu(unit, code);
  49.         fnb.data[fnb.len] = '\0';
  50.         if((ffp=fopen(fnb.data, "w"))!=NULL) return FIOSUC;
  51.     }
  52.     ewprintf("Cannot open file for writing");
  53.     return FIOERR;
  54. #else
  55.     if (code != 0 || (ffp=fdopen(open("", -2, unit), "w")) == NULL) {
  56.         ewprintf("Cannot open file for writing");
  57.         if(code==0) clo$fu(unit, code);
  58.         return (FIOERR);
  59.     }
  60.     return (FIOSUC);
  61. #endif
  62. }
  63.  
  64. /*
  65.  * Close a file.
  66.  * Should look at the status.
  67.  */
  68. ffclose()
  69. {
  70.     fclose(ffp);
  71.     return (FIOSUC);
  72. }
  73.  
  74. /*
  75.  * Write a buffer to the already
  76.  * opened file. bp points to the
  77.  * buffer. Return the status.
  78.  * Check only at the newline and
  79.  * end of buffer.
  80.  */
  81. ffputbuf(bp)
  82. BUFFER *bp;
  83. {
  84.     register char *cp;
  85.     register char *cpend;
  86.     register LINE *lp;
  87.     register LINE *lpend;
  88.  
  89.     lpend = bp->b_linep;
  90.     lp = lforw(lpend);
  91.     do {
  92.     cp = <ext(lp)[0];        /* begining of line    */
  93.     cpend = &cp[llength(lp)];    /* end of line        */
  94.     while(cp != cpend) {
  95.         putc(*cp, ffp);
  96.         cp++;    /* putc may evalualte arguments more than once */
  97.     }
  98.     lp = lforw(lp);
  99.     if(lp == lpend) break;        /* no implied newline on last line */
  100.     putc('\n', ffp);
  101.     } while(!ferror(ffp));
  102.     if(ferror(ffp)) {
  103.     ewprintf("Write I/O error");
  104.     return FIOERR;
  105.     }
  106.     return FIOSUC;
  107. }
  108.  
  109. /*
  110.  * Read a line from a file, and store the bytes
  111.  * in the supplied buffer. Stop on end of file or end of
  112.  * line.  When FIOEOF is returned, there is a valid line
  113.  * of data without the normally implied \n.
  114.  */
  115. ffgetline(buf, nbuf, nbytes)
  116. register char    *buf;
  117. register int    nbuf;
  118. register int    *nbytes;
  119. {
  120.     register int    c;
  121.     register int    i;
  122.  
  123.     i = 0;
  124.     while((c = getc(ffp))!=EOF && c!='\n') {
  125.         buf[i++] = c;
  126.         if (i >= nbuf) return FIOLONG;
  127.     }
  128.     if (c == EOF  && ferror(ffp) != FALSE) {
  129.         ewprintf("File read error");
  130.         return FIOERR;
  131.     }
  132.     *nbytes = i;
  133.     return c==EOF ? FIOEOF : FIOSUC;
  134. }
  135.  
  136. #ifndef NO_BACKUP
  137. /*
  138.  * Rename the file "fname" into a backup copy.
  139.  */
  140. fbackupfile(fname)
  141. char   *fname;
  142. {
  143.     struct {short len; char data[128];} back;
  144.     struct {short len; char data[32];} ent;
  145.     register char *cp;
  146.     short code = 0, i;
  147.  
  148.     strcpy(back.data, fname);
  149.     strcat(back.data, ".BAK");
  150.     (void) delete(back.data);
  151.     if((cp = rindex(back.data, '>'))!=NULL) {
  152.     if(back.data[0]==('<') && index(back.data, '>')==cp) {
  153.         strncpy(ent.data, back.data+1, ent.len = (cp - back.data) - 1);
  154.         at$abs((short)k$setc, ent, (short)0, code);
  155.     } else {
  156.         back.len = (cp - back.data);
  157.         if(index(back.data, '>') == cp)
  158.          at$any((short)k$setc, back, code);
  159.         else at$((short)k$setc, back, code);
  160.     }
  161.     if(code) return FALSE;
  162.     cp++;
  163.     /* cnam$$ needs word aligned strings */
  164.     strncpy(ent.data, cp, back.len = strlen(cp));
  165.     cp = ent.data;
  166.     } else back.len = strlen(cp = back.data);
  167.     cnam$$((char [])cp, (short)(back.len - 4), (char [])cp, back.len, code);
  168.     at$hom(i);
  169.     return code == 0;
  170. }
  171. #endif
  172.  
  173. /*
  174.  * The string "fn" is a file name.  Prepend the directory name if
  175.  * it's relative to the current directory.
  176.  */
  177. #ifndef NO_DIR
  178. extern char *wdir;
  179. #endif
  180.  
  181. char *adjustname(fn)
  182. register char  *fn;
  183. {
  184.     static char fnb[NFILEN];
  185.     register char *cp = fnb;
  186.     register char *cp2;
  187.  
  188. #ifndef NO_DIR
  189.     if((fn[0] == '*' && fn[1] == '>') || index(fn, '>')==NULL) {
  190.     cp2 = wdir;
  191.     while(*cp2) {
  192.         *cp = *cp2;
  193.         cp++;
  194.         cp2++;
  195.     }
  196.     *cp++ = '>';
  197.     if(fn[1]=='>') fn+=2;
  198.     }
  199. #endif
  200.     while(*fn) {
  201.     *cp = ISUPPER(*fn) ? TOLOWER(*fn) : *fn;
  202.     cp++;
  203.     fn++;
  204.     }
  205.     *cp = '\0';
  206.     return fnb;
  207. }
  208.  
  209. #ifndef NO_STARTUP
  210. char *startupfile(suffix)
  211. char *suffix;
  212. {
  213.     short code, len;
  214.     static char startname[128];
  215.     register char *cp;
  216.  
  217.     gpath$((short)k$inia, (short)-3, (char [])startname,
  218.           (short)(128 - 5), len, code);
  219.     if (code==0) {
  220.         strcpy(startname + len, ">.MG");
  221.         if(suffix) {
  222.         startname[len+4] = '.';
  223.         strcpy(startname + len + 5, suffix);
  224.         }
  225.         if(access(startname, 4)==0)
  226.         return startname;
  227.     }
  228.     strcpy(startname, "MG*>.MG");
  229.     if(suffix) {
  230.         startname[7] = '.';
  231.         strcpy(startname+8, suffix);
  232.     }
  233.     if(access(startname, 4)==0)
  234.         return startname;
  235.     return (char *)NULL;
  236. }
  237. #endif
  238.  
  239. /* compare file names */
  240. fncmp(fn1, fn2)
  241. register char *fn1, *fn2;
  242. {
  243.     /* ignore disk name if on one but not the other */
  244.     if(*fn1 != *fn2) {
  245.     if(*fn1 == '<') {
  246.         fn1 = index(fn1, '>');
  247.         if(fn1 == NULL) return -1;
  248.         fn1++;
  249.     } else if(*fn2 == '<') {
  250.         fn2 = index(fn2, '>');
  251.         if(fn2 == NULL) return -1;
  252.         fn2++;
  253.     } else return -1;
  254.     } else fn1++, fn2++;
  255.     /* compare ignoring case */
  256.     while(*fn1) {
  257.     if((*fn1 != *fn2) && (!ISUPPER(*fn1) || (TOLOWER(*fn1) == *fn2))
  258.               && (!ISUPPER(*fn2) || (TOLOWER(*fn2) == *fn1)))
  259.         return -1;
  260.     fn1++;
  261.     fn2++;
  262.     }
  263.     return *fn2;
  264. }
  265.  
  266. #ifndef NO_DIRED
  267. #include "kbd.h"
  268.  
  269. BUFFER *dired_(dirname)
  270. char *dirname;
  271. {
  272.     register BUFFER *bp;
  273.     LINE *lp, *blp;
  274.     BUFFER *findbuffer();
  275.     short dirunit, j, code, type, init, counts[4], i, nent;
  276.     struct {short len; char dat[128];} dir;
  277.     struct ent {short len; char dat[32];} base;
  278.     static struct ent wild[1] = {{2, {'@','@'}}};
  279.     static struct {
  280.     short vers;
  281.     struct ent *wp;
  282.     short count;
  283.     struct {unsigned dirs:1, seg_dirs:1, files:1, acats:1, rbf:1, spare:11;} desired;
  284.     long mb, ma, bb, ba, cb, ca, ab, aa;
  285.     } sel = {
  286.     1,
  287.     &wild[0],
  288.     1,
  289.     {1, 1, 1, 0, 0, 0},
  290.     0, 0, 0, 0, 0, 0, 0, 0,
  291.     }, *s = &sel;
  292. #define MAXSE 16
  293.     struct {
  294.     struct {unsigned type:8, length:8;} ecw;
  295.     struct ent name;
  296.     struct {unsigned spare:5, odel:1, owrite:1, oread:1, delp:1,
  297.         spare2:4, nodel:1, nowrite:1, noread:1;} prot;
  298.     struct {unsigned lrat:1, dumped:1, dos:1, spec:1, rwl: 2,
  299.         spare:2, type:8;} info;
  300.     long dtm;
  301.     short nondefault, logical_type, trunc;
  302.     long dtb, dtc, dta;
  303.     } ret[MAXSE], *r = &ret[0];
  304.     static char *types[] = {"SAM", "DAM", "SEGSAM", "SEGDAM", "UFD",
  305.     "ACAT", "CAM"};
  306.     static char *rwlock[] = {"sys", "excl", "updt", "none"};
  307.  
  308.     if((dirname = adjustname(dirname)) == NULL) {
  309.     ewprintf("Bad directory name");
  310.     return NULL;
  311.     }
  312.     (void) strncpy(dir.dat, dirname, dir.len = strlen(dirname));
  313.     if(dir.dat[dir.len-1]=='>') dir.len--;
  314.     else strcat(dirname, ">");
  315.     if((bp = findbuffer(dirname)) == NULL) {
  316.     ewprintf("Could not create directory buffer");
  317.     return NULL;
  318.     }
  319.     if(bclear(bp) != TRUE) return NULL;
  320.     (void) srsfx$((short)(k$read+k$getu), dir, dirunit, type,
  321.     (short)0, (short)0, base, j, code);
  322.     if(code!=0) {
  323.     ewprintf("Could not open directory '%s'", dirname);
  324.     return NULL;
  325.     }
  326.     if(type<2 || type > 4) {
  327.     clo$fu(dirunit, code);
  328.     ewprintf("Not a directory '%s'", dirname);
  329.     return NULL;
  330.     }
  331.     for(init = 0x8000; ; init = 0) {
  332.     dir$se(dirunit, type, init, s, r, (short)MAXSE,
  333.         (short)((sizeof ret[0])/2), nent, (short [])counts, (short)4, code);
  334.     if(code!=0 && (code!=e$eof || nent==0)) break;
  335.     for(i=0; i < nent; i++) {
  336.         cv$fdv(ret[i].dtm, j, base);
  337.         for(j=0; j<ret[i].name.len; j++)
  338.         if(ISUPPER(ret[i].name.dat[j]))
  339.             ret[i].name.dat[j] = TOLOWER(ret[i].name.dat[j]);
  340. #define D_FILEPOS 36
  341.         if((lp = lalloc(D_FILEPOS + 1 + ret[i].name.len)) != NULL) {
  342.         sprintf(lp->l_text, "  %-6s  %-4s  %.18s  %.*s",
  343.             types[ret[i].info.type], rwlock[ret[i].info.rwl],
  344.             base.dat, ret[i].name.len, ret[i].name.dat);
  345.         llength(lp)--;
  346.         for(blp=bp->b_linep->l_fp; blp!=bp->b_linep; blp=blp->l_fp) {
  347.             j = strncmp(ret[i].name.dat, &blp->l_text[D_FILEPOS],
  348.             ret[i].name.len);
  349.             if(j < 0 || (j==0 && ret[i].name.len <=
  350.             llength(blp)-D_FILEPOS)) break;
  351.         }
  352.         lp->l_fp = blp;
  353.         lp->l_bp = blp->l_bp;
  354.         blp->l_bp = lp;
  355.         lp->l_bp->l_fp = lp;
  356.         } else {
  357.         clo$fu(dirunit, code);
  358.         return NULL;
  359.         }
  360.     }
  361.     }
  362.     if(code!=e$eof) ewprintf("Directory read error %d", code);
  363.     clo$fu(dirunit, code);
  364.     bp->b_dotp = lforw(bp->b_linep);
  365.     bp->b_doto = 0;
  366.     bp->b_markp = NULL;
  367.     strncpy(bp->b_fname, dirname, NFILEN);
  368.     if((bp->b_modes[0] = name_mode("dired")) == NULL) {
  369.     bp->b_modes[0] = &map_table[0];
  370.     ewprintf("Could not find mode dired");
  371.     return NULL;
  372.     }
  373.     bp->b_nmodes = 0;
  374.     return bp;
  375. }
  376.  
  377. d_makename(lp, fn)
  378. register LINE *lp;
  379. register char *fn;
  380. {
  381.     register char *cp;
  382.  
  383.     if(llength(lp) <= D_FILEPOS) return ABORT;
  384.     (VOID) strcpy(fn, curbp->b_fname);
  385.     cp = fn + strlen(fn);
  386.     bcopy(&lp->l_text[D_FILEPOS], cp, llength(lp) - D_FILEPOS);
  387.     cp[llength(lp) - D_FILEPOS] = '\0';
  388.     return strncmp(&lp->l_text[2], "UFD", 3)==0 ||
  389.     strncmp(&lp->l_text[2], "SEG", 3)==0;
  390. }
  391.  
  392. rename(old, new)
  393. char *old, *new;
  394. {
  395.     struct {short len; char dat[128];} f;
  396.     struct {short len; char dat[32];} oldent;
  397.     char *cp;
  398.     short code;
  399.  
  400.     old = adjustname(old);
  401.     cp = rindex(old, '>');
  402.     strncpy(f.dat, old, f.len = cp - old);
  403.     at$((short)k$setc, f, code);
  404.     if(code!=0) return -1;
  405.     cp++;
  406.     strncpy(oldent.dat, cp, oldent.len = strlen(cp));
  407.     strncpy(f.dat, new, f.len = strlen(new));
  408.     cnam$$((char [])oldent.dat, oldent.len, (char [])f.dat, f.len, code);
  409.     if(code!=0) {
  410.     at$hom(code);
  411.     return -1;
  412.     }
  413.     at$hom(code);
  414.     return 0;
  415. }
  416. #endif
  417.  
  418. #ifndef NO_DIR
  419. char *getwd(cwd)
  420. char *cwd;
  421. {
  422.     char homedir[128];         /* cwd may not be word alligned */
  423.     short len, code;
  424.     register char *cp1, *cp2;
  425.  
  426.     gpath$((short)k$homa, (short)-2, (char [])homedir, (short)128, len, code);
  427.     if(code!=0) return NULL;
  428.     cp1 = cwd;
  429.     cp2 = homedir;
  430.     while(len--) {
  431.     *cp1 = ISUPPER(*cp2) ? TOLOWER(*cp2) : *cp2;
  432.     cp1++;
  433.     cp2++;
  434.     }
  435.     *cp1 = '\0';
  436.     return cwd;
  437. }
  438. #endif
  439.